//
//  LS_CityTravelProtocolImp.m
//  breadPro
//
//  Created by 刘文超 on 2019/7/5.
//  Copyright © 2019 刘文超. All rights reserved.
//

#import "LS_CityTravelProtocolImp.h"
#import "LS_BannerModel.h"
#import "LS_CityTravelItemModel.h"
@interface LS_CityTravelProtocolImp()

/**
 banner数组
 */
@property(nonatomic,strong)NSMutableArray *bannerData;

/**
 item 数组
 */
@property(nonatomic,strong)NSMutableArray *itemData;

/**
 首页model数据
 */
@property(nonatomic,strong)NSMutableDictionary *travelData;

@end

@implementation LS_CityTravelProtocolImp
-(RACSignal*)ls_requestCityTraveldataSignal:(NSString *)requestUrl
{
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        HTURLSessionTask *task= [HTNetWorking getWithUrl:requestUrl refreshCache:YES  showHUD:@"loading..." success:^(id response) {
            [self.bannerData removeAllObjects];
            [self.itemData removeAllObjects];
            NSDictionary *responseDic = response;
            NSArray *responseArr = responseDic[@"data"][@"elements"];
            [responseArr enumerateObjectsUsingBlock:^(NSDictionary *data, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([data[@"type"] integerValue] == 1) {// 广告数据
                    
                    [data[@"data"][0] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        LS_BannerModel *bannerModel = [LS_BannerModel mj_objectWithKeyValues:obj];
                        [self.bannerData addObject:bannerModel];
                    }];
                    
                }else if ([data[@"type"] integerValue] == 4){// item数据
                    
                    [data[@"data"] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        LS_CityTravelItemModel *bannerModel = [LS_CityTravelItemModel mj_objectWithKeyValues:obj];
                        [self.itemData addObject:bannerModel];
                    }];
                }
            }];
            [self.travelData setValue:self.bannerData forKey:BannerDataKey];
            [self.travelData setValue:self.itemData forKey:TravelDatakey];
            [subscriber sendNext:self.travelData];
            [subscriber sendCompleted];
            NSLog(@"data-----------%@",response);
        } fail:^(NSError *error) {
            [subscriber sendError:error];
        }];
    
        return [RACDisposable disposableWithBlock:^{
            [task cancel];
        }];
    }];
    
}

#pragma maek -
#pragma mark -- getter
-(NSMutableArray *)bannerData
{
    return LS_LAZY(_bannerData, @[].mutableCopy);
}
-(NSMutableArray *)itemData
{
    return LS_LAZY(_itemData, @[].mutableCopy);
}
-(NSMutableDictionary *)travelData
{
    return LS_LAZY(_travelData, @{}.mutableCopy);
}
@end
